package com.bjgzy.courselive.mvp.ui.fragment;

import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bjgzy.courselive.di.component.DaggerCourseIntroductionComponent;
import com.bjgzy.courselive.di.module.CourseIntroductionModule;
import com.bjgzy.courselive.mvp.contract.CourseIntroductionContract;
import com.bjgzy.courselive.mvp.model.entity.CourseIntroductionData;
import com.bjgzy.courselive.mvp.presenter.CourseIntroductionPresenter;
import com.bjgzy.courselive.mvp.ui.activity.CourseDetailsActivity;
import com.bjgzy.module_couselive.R;
import com.bjgzy.module_couselive.R2;
import com.blankj.utilcode.util.ObjectUtils;
import com.eduhzy.ttw.commonres.view.AutoConstraintLayout;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;
import im.delight.android.webview.AdvancedWebView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-31 15:49:54
 */
@Route(path = RouterHub.COURSELIVE_COURSEINTRODUCTIONFRAGMENT)
public class CourseIntroductionFragment extends BaseFragment<CourseIntroductionPresenter> implements CourseIntroductionContract.View {

    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.tv_project)
    TextView tvProject;
    @BindView(R2.id.tv_shop)
    TextView tvShop;
    @BindView(R2.id.ratingBar)
    RatingBar ratingBar;
    @BindView(R2.id.tv_study_no)
    TextView tvStudyNo;
    @BindView(R2.id.tv_price)
    TextView tvPrice;
    @BindView(R2.id.btn_buy)
    QMUIRoundButton btnBuy;
    @BindView(R2.id.tv_label)
    TextView tvLabel;
    @BindView(R2.id.tv_content)
    TextView tvContent;
    @BindView(R2.id.layout_content)
    AutoConstraintLayout layoutContent;
    @BindView(R2.id.webview)
    AdvancedWebView mWebView;
    private int  internalId;

    public static CourseIntroductionFragment newInstance() {
        CourseIntroductionFragment fragment = new CourseIntroductionFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerCourseIntroductionComponent
                .builder()
                .appComponent(appComponent)
                .courseIntroductionModule(new CourseIntroductionModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.courselive_fragment_introduction, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        btnBuy.setChangeAlphaWhenPress(true);
        if (internalId>=0) {
            mPresenter.getSummaryById(internalId);
        }

    }

    @Override
    public void setData(@Nullable Object data) {
        this.internalId = (int) data;
    }

    @Override
    public void showLoading() {
        layoutContent.setVisibility(View.GONE);
    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }


    @OnClick(R2.id.btn_buy)
    public void onViewClicked() {
        MobclickAgent.onEvent(getActivity(),"ttw_course_buy");
        ARouter.getInstance().build(RouterHub.COURSELIVE_ORDERPAYACTIVITY)
                .withString(Intent.EXTRA_TITLE, Constants.ORDERPAY)
                .withInt(Constants.ROUTER_INTEGER, internalId)
                .navigation();
    }

    @Override
    public void update(CourseIntroductionData data) {
        tvTitle.setText(data.getTitle());
        tvShop.setText(data.getShopName());
        tvProject.setVisibility(View.INVISIBLE);
        tvStudyNo.setText(String.format(getString(R.string.courselive_stydy_times), data.getViewTotal()));
        ratingBar.setRating(data.getStar());
        LayerDrawable ld_stars = (LayerDrawable) ratingBar.getProgressDrawable();
        ld_stars.getDrawable(2).setColorFilter(getActivity().getResources().getColor(R.color.public_color_orange), PorterDuff.Mode.SRC_ATOP);
        tvPrice.setText(ObjectUtils.isEmpty(data.getPrice()) ? "¥0" : "¥" + data.getPrice());
        tvContent.setText(data.getDescription());
        if (data.isIsBuy()) {
            btnBuy.setVisibility(View.INVISIBLE);
            ((CourseDetailsActivity) getActivity()).setJoinGone();
        }
        layoutContent.setVisibility(View.VISIBLE);
        if(ObjectUtils.isNotEmpty(data.getViewUrl())){
            mWebView.loadUrl(data.getViewUrl());
        }
    }

    @Override
    public void loadFail() {
    }
    @Subscriber(tag = RouterHub.COURSELIVE_COURSEINTRODUCTIONFRAGMENT)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.PAY_SUCCESS:
                if (internalId>=0) {
                    mPresenter.getSummaryById(internalId);
                }
                break;
            default:
        }
    }
}
